{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca2f57fd0f324b09ae2b0a4eccf31ad6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"Thin Plate Spline transformations describe a nonlinear warp\n",
    "transform defined by a set of source and target landmarks.\n",
    "Any point on the mesh close to a source landmark will\n",
    "be moved to a place close to the corresponding target landmark.\n",
    "The points in between are interpolated using Bookstein's algorithm. \"\"\"\n",
    "from vtkplotter import *\n",
    "import numpy as np\n",
    "np.random.seed(2)\n",
    "\n",
    "grids = []\n",
    "for i in range(10):\n",
    "    grids.append(Grid([0, 0, i / 10.0], resx=100, resy=100))\n",
    "act = mergeActors(grids)  # merge grids into a single object\n",
    "\n",
    "idxs = np.random.randint(0, act.N(), 10)  # pick 10 indexes\n",
    "\n",
    "ptsource, pttarget = [], []\n",
    "for i in idxs:\n",
    "    ptold = act.getPoint(i) + np.random.randn(3) * 0.02\n",
    "    ptsource.append(ptold)\n",
    "    ptnew = ptold + [0, 0, np.random.randn(1) * 0.10]  # move in z\n",
    "    pttarget.append(ptnew)\n",
    "\n",
    "warped = thinPlateSpline(act, ptsource, pttarget)\n",
    "warped.alpha(0.2).color(\"b\")\n",
    "\n",
    "arrs = Arrows(ptsource, pttarget)\n",
    "\n",
    "show(warped, arrs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
